/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "gpx.h"

GPX::GPX(QObject *parent) :
        QObject(parent)
{
}


void GPX::writeFile(QString src)
{
    int n_tracks,i;
    txt_file_name = src;

    /* load txt file */
    txt_file.readFile(txt_file_name);
    /* get num of tracks contained in file */
    n_tracks = txt_file.getNumTracks();

    /* write 1 file for each track */
    for(i=0;i<n_tracks;i++)
    {

        /* create output file */
        gpx_file_name = src + "_" + QString::number(i) +".gpx";
        gpx_file = new QFile(gpx_file_name);

        if (!gpx_file->open(QIODevice::WriteOnly | QIODevice::Text))
            return;

        /* write file */
        QTextStream out(gpx_file);
        out     << writeHeader()
                << writePoints(i)
                << writeFooter();

        gpx_file->close();
    }


}

QString GPX::writeHeader()
{
    QString temp;

    /* Standart header */
    temp.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" GPX_NL
            "<gpx version=\"1.1\" creator=\"Josep Vila\"" GPX_NL
            "  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\"" GPX_NL
            "  xmlns=\"http://www.topografix.com/GPX/1/1\"" GPX_NL
            "  xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"" GPX_NL
            "  xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" GPX_NL
            " <trk>" GPX_NL
            "  <name>KeyMaze 300 track</name>" GPX_NL
            "  <trkseg>"  GPX_NL);

    return temp;
}

QString GPX::writePoints(int trk_num)
{
    int i;
    QString temp="";
    QList< QList<TrackPoint *> > track;

    /* get tracks from file */
    track = txt_file.getTracks();

    /* get points of desired track */
    for(i=0; i< (track.at(trk_num)).count();i++)
    {
        temp.append( ( (track.at(trk_num) ).at(i))->getPoint() );
    }

    /* return gpx standard string containing points */
    return temp;
}

QString GPX::writeFooter()
{
    QString temp;

    /* standart footer */
    temp.append("    </trkseg>" GPX_NL
           "  </trk>" GPX_NL
           "</gpx>" GPX_NL);

    return temp;
}

